package com.itheima.test8;

public class StudentTest2 {
    public static void main(String[] args) {
        //要求5：查询数组id为2的学生，如果存在，则将他的年龄+1岁*/
        //定义数组
        Student[] arr = new Student[3];

        //创建学生对象
        Student stu1 = new Student(001, "张三", 20);
        Student stu2 = new Student(002, "李四", 19);
        Student stu3 = new Student(003, "王五", 21);

        //将对象放入数组
        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;
        //要求5：查询数组id为2的学生，如果存在，则将他的年龄+1岁*/
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                //存在
                if(arr[i].getId()==3){
                    //找到id为2，就将其年龄+1
                    int age=arr[i].getAge()+1;
                    //修改
                    arr[i].setAge(age);
                    //打印
                    printArr(arr);
                }
            }else {
                System.out.println("该学生不存在");
            }
        }

    }
    //遍历输出数组的方法
    public static void printArr(Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                System.out.println(arr[i].getId() + " " + arr[i].getName() + " " + arr[i].getAge());
            }
        }
    }

}
